var express = require('express')
var router = express.Router()

let houseparentData = [
    {houseparentId: '222201', houseparentName: '李振威', sex: '1', age: '45', identityCard: '100000000000000001', tel: '16688002677'},
    {houseparentId: '222202', houseparentName: '胡禹帆', sex: '1', age: '40', identityCard: '100000000000000002', tel: '16688002677'},
    {houseparentId: '222203', houseparentName: '罗文杰', sex: '1', age: '42', identityCard: '100000000000000003', tel: '16688002677'},
    {houseparentId: '222204', houseparentName: '伍广', sex: '2', age: '48', identityCard: '100000000000000004', tel: '16688002677'},
    {houseparentId: '222205', houseparentName: '李松', sex: '2', age: '46', identityCard: '100000000000000005', tel: '16688002677'},
    {houseparentId: '222206', houseparentName: '雷涛', sex: '1', age: '53', identityCard: '100000000000000006', tel: '16688002677'},
    {houseparentId: '222207', houseparentName: '姜夏', sex: '2', age: '52', identityCard: '100000000000000007', tel: '16688002677'},
    {houseparentId: '222208', houseparentName: '王作钢', sex: '1', age: '43', identityCard: '100000000000000008', tel: '16688002677'},
]

router.get('/list', function (req, res, next) {
    if (!req.query.pageIndex) {
        res.send('缺少pageIndex')
    }

    if (!req.query.pageSize) {
        res.send('缺少pageSize')
    }

    let data = houseparentData.filter(item => {
        return item.houseparentId.includes(req.query.houseparentId) && item.houseparentName.includes(req.query.houseparentName)
    })

    let start = (Number(req.query.pageIndex) - 1) * req.query.pageSize
    let end = Number(req.query.pageIndex) * req.query.pageSize

    res.send({
        pageIndex: req.query.pageIndex,
        pageSize: req.query.pageSize,
        total: data.length,
        data: data.slice(start, end)
    })
})

router.post('/add', function (req, res, next) {

    let data = {
        houseparentId: req.body.houseparentId,
        houseparentName: req.body.houseparentName,
        sex: req.body.sex,
        age: req.body.age,
        identityCard: req.body.identityCard,
        tel: req.body.tel
    }

    houseparentData.push(data)

    res.send({
        msg: '新增成功',
        code: 200,
        data: true
    })
})

router.put('/update', function (req, res, next) {
    if (!req.body.houseparentId) {
        res.send('缺少id字段')

        return
    }

    let index = houseparentData.findIndex(item => {
        return String(item.houseparentId) === String(req.body.houseparentId)
    })

    console.log('index',index)
    if (index < 0) {
        res.send({
            msg: '编辑失败，未通过houseparentId找到对应数据',
            code: 500,
            data: false
        })

        return
    }

    houseparentData[index] = Object.assign({}, req.body)

    res.send({
        msg: '编辑成功',
        code: 200,
        data: true
    })
})

router.delete('/delete', function (req, res, next) {
    console.log(req.query)
    if (!req.query.houseparentId) {
        res.send('缺少houseparentId字段')
    }

    let index = houseparentData.findIndex(item => {
        return Number(item.houseparentId) === Number(req.query.houseparentId)
    })

    if (index < 0) {
        res.send({
            msg: '删除失败，未能通过houseparentId找到对应数据',
            code: 500,
            data: false
        })
        return
    }

    houseparentData.splice(index, 1)

    res.send({
        msg: '删除成功',
        code: 200,
        data: true
    })
})

module.exports = router